<%@ page import="com.ecomm.util.Config,
				 com.ecomm.orgmodel.IOrgModel,
				 com.ecomm.orgmodel.DefaultOrgModelImpl,
				 com.ecomm.util.DB,
				 com.ecomm.util.DBFactory,
				 com.ecomm.util.Function,
				 com.ecomm.orgmodel.OrgInfo,
				 com.ecomm.security.ACL,
				 com.ecomm.portal.portalconfig.PortalInfo,
				 com.ecomm.portal.portalconfig.PortalConfFactory,
				 com.ecomm.portal.portaltree.IPortalTreeRight,
				 com.ecomm.session.SessionMng,
                 java.util.ArrayList,
                 java.util.StringTokenizer,
                 java.util.Collection,
                 java.util.Iterator,
                 com.ecomm.rtx.RtxDataModel"%>
<%@ page contentType="text/html;charset=utf-8"  language="java"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

<%@ include file="/include/filepath.jsp"%>
<%--<%@ include file="/include/currentuserinfo.jsp" %>--%>

<%
    SessionMng sessionmng=(SessionMng)session.getAttribute("sessionmng");
    String userid= null;
    String userloginname = "";

    if(sessionmng != null)
    {
        userid=sessionmng.getUserInfo().getID();
        userloginname = sessionmng.getUserInfo().getLoginName();
    }

    //判断是否登陆成功
    String loginstatus = (String)request.getAttribute("LoginStatus");
    String accreditloginstatus = Function.normalizeString((String)session.getAttribute("AccreditLoginStatus"));
    session.removeAttribute("AccreditLoginStatus");

    request.removeAttribute("LoginStatus");
    if (loginstatus!=null)
    {
        String errormessage = "";
        if("none".equals(loginstatus))
            errormessage = "用户不存在或出现内部错误！";
        if("free".equals(loginstatus))
            errormessage = "用户为自由用户，限制登录！";
        if ("login".equals(loginstatus))
            errormessage = "您输入的密码不正确，请重新输入！";
        if ("license".equals(loginstatus))
            errormessage = "您所使用的许可证非法或过期，请与厂商索取新的许可证！";
        if ("nolicense".equals(loginstatus))
            errormessage = "许可证不存在，请与厂商索取许可证！";
%>
<script language="JavaScript">
alert("<%=errormessage%>");
</script>
<%
    }
    if((RtxDataModel.RTX_ENABLED)&&(accreditloginstatus.equals("")))
    {
%>

<%--<%@include file="/rtx/rtxlogin.jsp" %>--%>

<!--此页面的执行顺序是： homepage.jsp----Tiles的taglib----读取oaclassic-defs.xml----classiclayout.jsp----DisplayChannelAction----输出html页面-->
<%
    }
    if (Function.normalizeString((String)request.getAttribute("rtxerrormsg")).equals("rtxerror"))
    {
        request.removeAttribute("rtxerrormsg");
%>
    <script language="JavaScript">
    alert("请检查RTX配置：\n 1.检查config.properties中RTX的设置\n 2.检查SDK安装目录下的文件RtxServerApi.ini的设置\n 3.检查SDKAPIJava.dll所在文件夹是否加入path中");
    </script>
<%
    }
	String infoissue = request.getParameter("infoissue");
	String infoissue_mainportal_id = Config.getString("portal.infoissue.mainportalid");
    String portal_id = null;
	DB db = null;
    db = DBFactory.getDBInstance();
    session.setAttribute("db",db);

	if (userid==null) //用户未登录
	{
		portal_id = infoissue_mainportal_id;
        request.setAttribute("portal_id",portal_id);
	}
	else //用户已经登录
	{
        IOrgModel iOrgModel = new DefaultOrgModelImpl(db);
        ACL portalAcl = new ACL(db);
        if(infoissue != null){

            //1、取得用户自己具有查看权限的信息发布门户的ID
            portal_id = portalAcl.getPortal_id(userid,IPortalTreeRight.INFOISSUE_USING,"portaltree");

            //2、如果用户自己没有定义信息发布门户，则取得离用户最近的祖先组织对应的信息发布门户的ID
            if (portal_id == null)
            {
                //取得用户在组织机构中的路径
                ArrayList lst = new ArrayList();
                String pathofuser = iOrgModel.getPathOfUser(userid);
                StringTokenizer token = null;
                if (pathofuser!=null)
                {
                    token = new StringTokenizer(pathofuser,"/");
                    while(token.hasMoreTokens())
                    {
                        lst.add(token.nextToken());
                    }
                }

                //查找此用户的父组织对应的门户，如果没有，则依次向上检索该用户的所有祖先组织是否有对应的登录门户
                if (lst.size() > 0)
                {
                    int k = 0;
                    for(int i = lst.size()-1; i >= 0;i--)
                    {
                        portal_id = portalAcl.getPortal_id(lst.get(i).toString(),IPortalTreeRight.INFOISSUE_USING,"portaltree");
                        if (portal_id!=null)
                        {
                            //找到离此用户最近的祖先组织对应的门户，作为此用户的查看发布信息的主门户
                            k = 1;
                            break;
                        }
                    }
                    //如果当前用户的所有祖先组织都没有定义信息发布门户，则访问默认的门户
                    if (k == 0)
                    {
                        portal_id = infoissue_mainportal_id;
                    }
                }
                else //此用户是自由用户，不隶属于任何组织
                {
                    portal_id = infoissue_mainportal_id;
                }

            }
        }else{
            portal_id = portalAcl.getPortal_id(userid,IPortalTreeRight.WORKPLACE_USING,"portaltree");

            //2、如果用户自己没有定义信息发布门户，则取得离用户最近的祖先组织对应的信息发布门户的ID
            if (portal_id == null)
            {
                //取得用户在组织机构中的路径
                ArrayList lst = new ArrayList();
                String pathofuser = iOrgModel.getPathOfUser(userid);
                StringTokenizer token = null;
                if (pathofuser!=null)
                {
                    token = new StringTokenizer(pathofuser,"/");
                    while(token.hasMoreTokens())
                    {
                        lst.add(token.nextToken());
                    }
                }

                //查找此用户的父组织对应的门户，如果没有，则依次向上检索该用户的所有祖先组织是否有对应的登录门户
                if (lst.size() > 0)
                {
                    int k = 0;
                    for(int i = lst.size()-1; i >= 0;i--)
                    {
                        portal_id = portalAcl.getPortal_id(lst.get(i).toString(),IPortalTreeRight.WORKPLACE_USING,"portaltree");
                        if (portal_id!=null)
                        {
                            //找到离此用户最近的祖先组织对应的门户，作为此用户的查看发布信息的主门户
                            k = 1;
                            break;
                        }
                    }
                    //如果当前用户的所有祖先组织都没有定义信息发布门户，则访问默认的门户
                    if (k == 0)
                    {
                        portal_id = infoissue_mainportal_id;
                    }
                }
                else //此用户是自由用户，不隶属于任何组织
                {
                    portal_id = infoissue_mainportal_id;
                }

            }
        }
    }

	//频道调用子门户时，不检查权限
	String refPortal_id = Function.normalizeString(request.getParameter("refPortal_id"));
    if(!refPortal_id.equals(""))
    {  
        portal_id =refPortal_id;
        infoissue="true";
        request.setAttribute("isCheckRight","false");
    }
	
	request.setAttribute("portal_id",portal_id);
		
	/**
	 * 转发的DisplayChannelAction时，作为判断读取xsl模板的参数，pagefrom＝“prehomepage”
	 * 时读取template_custom.xsl的模板; pagefrom="homepage"时，读取template.xsl的模板
	 */
	request.setAttribute("pagefrom","homepage");
	
	//从portal对应的xml文件中读取tilesdefinition的值
	String definition = null;
    //System.out.println(portal_id);
	PortalInfo  portalInfo = PortalConfFactory.getPortalConfig(portal_id);
    Collection colPortalId = (Collection)session.getAttribute("colPortalId");
    if(userid != null && colPortalId != null){
        boolean bHasNoPortal = true;
        if(colPortalId.size() == 1 && null == portalInfo)
            bHasNoPortal = true;
        else if(colPortalId.size() > 1 &&  null == portalInfo){
            for(Iterator it = colPortalId.iterator();it.hasNext();){
                portalInfo = PortalConfFactory.getPortalConfig(Function.normalizeString((String)it.next()));
                if(null == portalInfo) continue;
                bHasNoPortal = false;
                break;
            }

        }else if(null == portalInfo){
            bHasNoPortal = true;
        }else{
            bHasNoPortal = false;
        }

        if(bHasNoPortal){
            out.println("<script>alert('工作台门户错误:门户框架尚未设置！请在门户管理中设置。');location.href='/oa/oaindex.jsp';</script>");
        }
    }
	if (portalInfo!=null)
	{
		definition = portalInfo.getTilesdefinition();
		if (definition!=null)
		{
			if (userid == null)
			{
				definition = definition + "_infoissue";
			}
			else
			{
				if (infoissue == null)
				{
					definition = definition + "_workplace";
				}
				else
				{
					definition = definition + "_infoissue";
				}
			}
		}
        //System.out.println(definition);
	}


    if(definition !=null){
%>
<!--当前门户对应的首页总体布局，对应于oaclassic-defs.xml文件中threeframe、twoframe两种布局-->

<tiles:insert definition="<%=definition%>">
</tiles:insert>


<%
    }else{

    }
	if (session.getAttribute("db") != null)
    {
        session.removeAttribute("db");
    }
    if (db!=null)
	{
		db.close();
	}
%>
